package com.zhuzhe.blog.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.convert.Convert;
import com.zhuzhe.blog.system.entity.SysRole;
import com.zhuzhe.blog.system.entity.SysUser;
import com.zhuzhe.blog.system.mapper.SysUserMapper;
import com.zhuzhe.blog.system.requestDto.SysUserPageDto;
import com.zhuzhe.blog.system.responseVo.SysRoleListVo;
import com.zhuzhe.blog.system.responseVo.SysUserListVo;
import com.zhuzhe.blog.system.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author u53
 * @since 2020-10-13
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public ResponseData<IPage<SysUserListVo>> queryPage(SysUserPageDto sysUserPageDto) {

        //条件查询
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPageDto.getUsername()),"username", sysUserPageDto.getUsername());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserPageDto.getMobile()),"mobile", sysUserPageDto.getMobile());
        queryWrapper.orderByDesc("update_date");
        IPage<SysUser> sysUserIPage = baseMapper.selectPage(sysUserPageDto.getPage(), queryWrapper);
        //返回封装
        IPage<SysUserListVo> sysUserListVoIPage = new Page<>();
        sysUserListVoIPage.setRecords(Convert.convertList(sysUserIPage.getRecords(), SysUserListVo.class));
        return JsonData.success(sysUserListVoIPage);
    }

    @Override
    public ResponseData<List<String>> findRoleIdsByid(String id) {
        List<String> ids = baseMapper.findRoleIdsByid(id);
        return JsonData.success(ids);
    }

    @Override
    public ResponseData<Integer> deleteUserRoleByUserId(String userId) {

        Integer result = baseMapper.deleteUserRoleByUserId(userId);
        return JsonData.success(result);
    }

    @Override
    @Transactional
    public ResponseData<Integer> saveUserRole(String userId, List<String> roleIds) {
        //删除角色用户
        baseMapper.deleteUserRoleByUserId(userId);
        if (CollectionUtils.isNotEmpty(roleIds)){
            baseMapper.saveUserRole(userId,roleIds);
        }
        return JsonData.success();
    }

    @Override
    public SysUser findByUsername(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq(SysUser.USERNAME, username);
        return baseMapper.selectOne(wrapper);
    }

}
